package com.hsmpay.mobile.action.added;

import com.hsmpay.common.pojo.BasePojo;
import com.hsmpay.common.pojo.qian4.Qian4SendLog;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.common.util.NumberUtil;
import com.hsmpay.common.util.OrderTypeUtil;
import com.hsmpay.common.util.system.Constant;
import com.hsmpay.mobile.action.account.fc.PurchaseBaseAction;
import com.hsmpay.mobile.action.base.MobileBaseAction;
import com.hsmpay.mobile.ds.DS_ECBService;
import com.hsmpay.mobile.ds.DS_IdeaioeService;
import com.hsmpay.mobile.ds.DS_RuiShengService;
import com.hsmpay.mobile.pojo.added.AddedOrder;
import com.hsmpay.mobile.pojo.terminal.Tchannel;
import com.hsmpay.mobile.service.added.AddedOrderService;
import com.hsmpay.mobile.service.terminal.TchannelService;
import com.hsmpay.mobile.service.trans.Qian4SendLogService;
import com.hsmpay.mobile.service.trans.TransRefuseCardService;
import com.hsmpay.qian4.pojo.PurchaseResult;
import com.hsmpay.qian4.pojo.RevocationResult;
import com.hsmpay.qian4.service.ds.ZYTLPayService;
import com.hsmpay.utils.TimeUtils;
import com.hsmpay.mobile.ds.DS_RuiShengGTService;
import com.hsmpay.utils.system.ConConfig;


import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AddedBaseAction extends MobileBaseAction {
    private static final long serialVersionUID = 8745625548239877562L;

    @Resource(name = "transRefuseCardService")
    protected TransRefuseCardService<BasePojo, Long> transRefuseCardService;//拒绝卡号交易
    @Resource(name = "addedOrderService")
    protected AddedOrderService<AddedOrder, Long> addedOrderService;
    @Resource(name = "qian4SendLogService")
    protected Qian4SendLogService<Qian4SendLog, Long> qian4SendLogService;
    @Resource(name = "tchannelService")
    protected TchannelService<Tchannel, Long> tchannelService;
    @Resource(name = "ds_ECBService")
    public DS_ECBService<BasePojo, Long> ds_ECBService;//ECB
    @Resource(name = "ds_zytlPayService")
    public ZYTLPayService ds_zytlPayService;
    @Resource(name = "ds_RuiShengService")
    public DS_RuiShengService<BasePojo, Long> ds_RuiShengService;//瑞升
    @Resource(name="ds_RuiShengGTService")
    public DS_RuiShengGTService<BasePojo,Long> ds_RuiShengGTService;//瑞晟国通
    @Resource(name="ds_IdeaioeService")
    public DS_IdeaioeService<BasePojo,Long> ds_IdeaioeService;//E点支付

    protected static final String Certificate = Constant.getAttribute("CERTIFICATE_BASE_URL") + "addc.action?d=";//小票 base路径

    protected String amount;//支付金额
    protected String merchantOrderTime;//商户订单时间 yyyyMMddHHmmss
    protected String accountNumber;//主账户
    protected String pin;//密码
    protected String track2Data;//磁道2数据
    protected String track3Data;//磁道3数据
    protected String signaturePath;//签名
    protected String orderNum;
    protected String encryType;//
    protected String icCardYu;//IC卡域
    protected String transTerminalId;//翰星中单ID

    public static Map<Long, Object> orderType = new HashMap<Long, Object>();

    static {
        orderType.put(60L, "缴水费");
        orderType.put(61L, "缴电费");
        orderType.put(62L, "缴煤费");
        orderType.put(63L, "违章代办");
        orderType.put(65L, "话费充值");
        orderType.put(66L, "固话充值");
        orderType.put(67L, "宽带充值");
    }


    /**
     * 获得订单编号
     *
     * @param date
     * @return
     */
    protected String getOrderNumByDate(Date date) {
        StringBuilder sb = new StringBuilder();
        String dt = DateConvertUtil.dateString(date, "yyyyMMddHHmmss");//获得日期字符串
        //String userIdStr = NumberUtil.supplyAll(userId,10);//获得10位userId
        long r = (long) (Math.random() * 1000000);//获得六位随机数
        String rStr = NumberUtil.supplyAll(r, 4);
        return sb.append(dt).append(rStr).toString();
    }


    /**
     * 撤销超时
     *
     * @return
     * @throws Exception
     */
    public Map<String, Object> revocationOrderTimeOut(Qian4SendLog recSend, Long orderId) throws Exception {

        Date date = new Date();
        Qian4SendLog send = new Qian4SendLog();
        send.setCallType(0);
        send.setUserId(recSend.getUserId());
        send.setApplication("Void.Req");
        send.setOrderTypeId(OrderTypeUtil.REVOCATION);
        send.setSendTime(date);
        send.setSendSeqId(TimeUtils.getTimeToString());
        send.setMerchantId(recSend.getMerchantId());//商户代码 ##
        send.setTerminalId(recSend.getTerminalId());
        send.setPlatformId(recSend.getPlatformId());//平台ID  ##
        send.setTransType("200000");//200000：撤销

        send.setMerchantOrderId(DateConvertUtil.dateString(date, "yyyyMMddHHmmssSSS"));
        send.setMerchantOrderTime(DateConvertUtil.stringDate(merchantOrderTime, "yyyyMMddHHmmss"));
        send.setTransTime(send.getSendTime());
        send.setOrgTransId(recSend.getBankOrderNum());//原订单号
        send.setOrgTransTime(recSend.getMerchantOrderTime());//原订单时间
        send.setOrgMerchantOrderId(recSend.getMerchantOrderId());
        send.setTransAmt(recSend.getTransAmt());

        send.setAccountNumber(recSend.getAccountNumber());//卡号  安心一百 需要
        send.setTrack2Data(recSend.getTrack2Data());
        send.setTrack3Data(recSend.getTrack3Data());
        send.setPin(recSend.getPin());

        send.setMerchantSign(recSend.getMerchantSign());
        send.setSenderSign(recSend.getSenderSign());
        send.setServiceInfo(recSend.getServiceInfo());
        send.setTerminalInfo(recSend.getTerminalInfo());
        //send.setMac(mac);
        send.setBankRef(recSend.getBankRef());
        send.setCollectionType(recSend.getCollectionType());
        send.setBankOrderNum(recSend.getBankOrderNum());
        send.setBatchId(recSend.getBatchId());
        send.setAuthorizationCode(recSend.getAuthorizationCode());

        send.setIcCardYu(recSend.getIcCardYu());//IC卡域

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("status", 0);
        try {
            RevocationResult rs = null;//lianYiJiaService.unfdPay(send,bankRef);
            switch (recSend.getCollectionType()) {
                case 33:
                    rs = ds_ECBService.unBillPayment(send);
                    break;//ECB
                case 34:
                    rs = ds_RuiShengService.unBillPayment(send);
                    break;//瑞升
                default:
                    rs = ds_ECBService.unBillPayment(send);
                    break;//ECB
            }
            int orderStatus = 7;//发起撤销
            if (1 == rs.getStatus() && "0000".equals(rs.getReqCode())) {
                orderStatus = 6;
            }
            qian4SendLogService.updateResCodeMassage(send.getId(), rs.getReqCode(), rs.getReqMsg());
            addedOrderService.updateRevoCodeAndMsg(orderId, orderStatus, rs.getReqCode(), rs.getReqMsg(), send.getMerchantOrderId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 去支付
     *
     * @param send
     * @param order
     * @return
     * @throws Exception
     */
    protected PurchaseResult payOrder(Qian4SendLog send, AddedOrder order) throws Exception {
        //安心一百 socket 参数
        Tchannel tc = tchannelService.searchMobileTchannelByCode(send.getCollectionType());
//		if(15 == send.getCollectionType()){
//			send = anxinybService.initSend(send, tc.getOtherParam());
//		}else if(1 == send.getCollectionType()){
//			send = hanxinService.purchaseJiaMi(send, 0.004);
//		}
        if (PurchaseBaseAction.TC_ECB == send.getCollectionType()) {//ECB
            send = ds_ECBService.initSend(send, tc);
        } else if (PurchaseBaseAction.TC_RuiSheng == send.getCollectionType()) {//瑞升
            send = ds_RuiShengService.initSend(send, tc);
        }else if(PurchaseBaseAction.TC_RuiSheng_GT == send.getCollectionType()){//瑞升国通
            send = ds_RuiShengGTService.initSend(send,tc);
        }else if(PurchaseBaseAction.TC_EPOINT == send.getCollectionType()){//E点
            send = ds_IdeaioeService.initSend(send,tc);
        }else if (PurchaseBaseAction.TC_ZHONGYI == send.getCollectionType()) {//case PurchaseBaseAction.TC_ZHONGYI:
            long amtLimit = Long.valueOf(ConConfig.getAttribute("ZYTL_FDORFL_AMT_LIMIT"));
            long transMoney = Long.valueOf(send.getTransAmt());
            if(transMoney > amtLimit){
                send.setRoleId(3l);
            }else {
                send.setRoleId(2l);
            }
            send = ds_zytlPayService.initSend(send);
        }

        long sendId = addedOrderService.insertOrder(send, order);
        send.setId(sendId);

        PurchaseResult rs = null;

        switch (send.getCollectionType()) {
            case 33:
                rs = ds_ECBService.billPayment(send, tc);
                break;//ECB
            case 34:
                rs = ds_RuiShengService.billPayment(send, tc);
                break;//ECB
            case 36:
                long amtLimit = Long.valueOf(ConConfig.getAttribute("ZYTL_FDORFL_AMT_LIMIT"));
                long transMoney = Long.valueOf(send.getTransAmt());
                if(transMoney > amtLimit){
                    send.setRoleId(3l);
                }else {
                    send.setRoleId(2l);
                }
                rs = ds_zytlPayService.purchase(send, order.getSignaturePath());
                break;
            case 38:
                rs = ds_RuiShengGTService.billPayment(send,tc);
                break;//瑞晟国通
            case 40:
                rs = ds_RuiShengGTService.billPayment(send,tc);
                break;//E点
            default:
                rs = ds_ECBService.billPayment(send, tc);//ECB
        }
        return rs;
    }
}
